<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Axios</title>
    <script type="text/javascript" src="js/vue.js"></script>
    <script type="text/javascript" src="js/axios.min.js"></script>
</head>
<body>
<div id="d1">
    用户名:<input type="text" v-model="user.username"><br/>
    密&nbsp;&nbsp;&nbsp;码:<input type="password" v-model="user.password"/><br/>
    <input type="button" value="get请求" @click="loginGet"/>
    <input type="button" value="post请求" @click="loginPost"/>
    <input type="button" value="put请求" @click="loginPut"/>
    <input type="button" value="delete请求" @click="loginDelete"/>
</div>
<script>
    new Vue({
        el:"#d1",
        data:{
            user:{
                id:"2",
                username:"vue",
                password:"666666"
            }
        },
        created(){
            this.login();
        },
        methods:{
            login(){
                axios.get("http://localhost:8080/users/f1").then(result => {
                    console.log(result.data);
                })
            },
            loginGet(){
                axios.get('http://localhost:8080/users/f2',{
                    params:this.user
                }).then(result => {
                    console.log(result.data);
                })
            },
            loginPost(){
                // URLSearchParams:用于处理post请求中的参数
                // 默认情况下,post请求参数会以JSON格式进行传递
                // 而我们需要的是表单数据
                // 通过URLSearchParams对传递的数据进行处理
                let params = new URLSearchParams();
                params.append("id",this.user.id);
                params.append("username",this.user.username);
                params.append("password",this.user.password);
                axios.post('http://localhost:8080/users/f2',params).then(result => {
                    console.log(result.data);
                })
            },
            loginPut(){
                let params = new URLSearchParams();
                params.append("id",this.user.id);
                params.append("username",this.user.username);
                params.append("password",this.user.password);
                axios.put('http://localhost:8080/users/f2',params).then(result => {
                    console.log(result.data);
                })
            },
            loginDelete(){
                axios.delete('http://localhost:8080/users/f2',{
                    params:this.user
                }).then(result => {
                    console.log(result.data);
                })
            },
        }
    });
</script>
</body>
</html>